var vm_gonggaoView = new Vue({
    el: "#app",
    data(){
        return {
            gonggaoList: [],
            gonggaoId: "",
            gonggao: {},
            zhengceList:[],
            zhinanList:[],
            showzhengcetongzhi:false,                //初始化显示政策通知
            showbanshizhinan:false,                //初始化隐藏办事指南

            type : $("#type").val(),


            //总项目数
            totalCount: 200,
            //分页数
            pageCount: 20,
            //当前页面
            pageCurrent: 1,
            //分页大小
            pagesize: 10,
            //显示分页按钮数
            showPages: 11,
            //开始显示的分页按钮
            showPagesStart: 1,
            //结束显示的分页按钮
            showPageEnd: 100,
            //分页数据
            arrayData: []


        }
    },

    created : function(){
        var _this = this

        if (_this.type=='banshizhinan'){
            _this.showzhengcetongzhi=true;
            _this.bindgonggaoList()
        }else if (_this.type=='zhengcetongzhi'){
            _this.showbanshizhinan=true;
            _this.bindgonggaoList()
        }
        _this.showPage(this.pageCurrent,  true);
        //_this.checkDeptInfo()
    },
    methods : {
        checkDeptInfo :function (){
            $.get("/system/deptdetail/checkDeptInfo",{},function (ppData){
                    if(ppData.result == "0"){
                        layer.alert(ppData.message, {
                            skin: 'layui-layer-molv' //样式类名
                            ,closeBtn: 0,icon: 6
                        }, function(){
                            $.modal.parentTab("本单位信息","/system/deptdetail/customedit");
                            layer.close(index); //如果配置了回调方法，就需要手动关闭提示框
                        });
                    }
                }
                ,"json")
        },
        bindgonggaoList: function (){
            var _this = this;
            $.ajaxSettings.async = false;
            $.post("/system/gonggaoView/list",{},function (ppData){
                if(ppData != null){
                    _this.gonggaoList = ppData;
                    var gonggaoList = _this.gonggaoList
                    for(key in gonggaoList){
                        if (gonggaoList[key].gonggaoleixing=="1"){
                            _this.zhengceList.unshift(gonggaoList[key])
                        }else if (gonggaoList[key].gonggaoleixing=="2"){
                            _this.zhinanList.unshift(gonggaoList[key])
                        }
                    }
                    _this.totalCount=_this.zhengceList.length
                    _this.pageCount=Math.ceil(_this.totalCount / _this.pagesize)
                    }else{
                        layer.alert(ppData.msg);
                    }
                }
            ,"json")
            $.ajaxSettings.async = true;
        },
        showzhinan(){
            var _this = this
            _this.showzhengcetongzhi=false
            _this.showbanshizhinan=true
/*            $("#zhengce").removeAttr("class")
            $("#zhinan").attr("class","cur")*/
            _this.totalCount=_this.zhinanList.length
            _this.pageCount=Math.ceil(_this.totalCount / _this.pagesize)
            _this.showPage(1,  true);
        },
        showzhengce(){
            var _this = this
            _this.showzhengcetongzhi=true
            _this.showbanshizhinan=false
/*            $("#zhengce").attr("class","cur")
            $("#zhinan").removeAttr("class")*/
            _this.totalCount=_this.zhengceList.length
            _this.pageCount=Math.ceil(_this.totalCount / _this.pagesize)
            _this.showPage(1,  true);
        },
        gonggaoDetail(ppgonggaiid,ppgonggaobiaoti){
            var gonggaoid = ppgonggaiid
            var gonggaobiaoti = ppgonggaobiaoti
            if(ppgonggaobiaoti.length>5)
                gonggaobiaoti = ppgonggaobiaoti.substring(0,5) +"..."
            $.modal.openTab(gonggaobiaoti,"system/gonggaoView/detail?gonggaoid="+gonggaoid,false)
        },

        showPage: function (pageIndex, forceRefresh) {
            var _this = this
            if (pageIndex > 0) {

                if (pageIndex > this.pageCount) {
                    pageIndex = this.pageCount;
                }


                //处理分页点中样式
                var buttons = $("#pager").find("span");
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons.eq(i).html() != pageIndex) {
                        buttons.eq(i).removeClass("active");
                    }
                    else {
                        buttons.eq(i).addClass("active");
                    }
                }

                if(_this.showzhengcetongzhi) {
                    var newPageInfo = [];
                    _this.pageCurrent = pageIndex;
                    newPageInfo = _this.zhengceList.slice((_this.pageCurrent - 1) * _this.pagesize, _this.pageCurrent * _this.pagesize)
                    _this.arrayData = newPageInfo;
                }
                if(_this.showbanshizhinan) {
                    var newPageInfo = [];
                    _this.pageCurrent = pageIndex;
                    newPageInfo = _this.zhinanList.slice((_this.pageCurrent - 1) * _this.pagesize, _this.pageCurrent * _this.pagesize)
                    _this.arrayData = newPageInfo;
                }
                //计算分页按钮数据
                if (this.pageCount > this.showPages) {
                    if (pageIndex <= (this.showPages - 1) / 2) {
                        this.showPagesStart = 1;
                        this.showPageEnd = this.showPages - 1;
                        console.log("showPage1")
                    }
                    else if (pageIndex >= this.pageCount - (this.showPages - 3) / 2) {
                        this.showPagesStart = this.pageCount - this.showPages + 2;
                        this.showPageEnd = this.pageCount;
                        console.log("showPage2")
                    }
                    else {
                        console.log("showPage3")
                        this.showPagesStart = pageIndex - (this.showPages - 3) / 2;
                        this.showPageEnd = pageIndex + (this.showPages - 3) / 2;
                    }
                }
                console.log("showPagesStart:" + this.showPagesStart + ",showPageEnd:" + this.showPageEnd + ",pageIndex:" + pageIndex);
            }

        },

        showPagebefore(){
            var _this = this
            if(_this.showzhengcetongzhi){
                _this.showzhengce()
            }else {
                _this.showzhinan()
            }
        }


    }
})